{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "###########调包\n",
    "import os\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from datetime import *\n",
    "import time\n",
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "############数据文件文件路径\n",
    "train_dir = '../../contest/train/'\n",
    "test_dir = '../../contest/A榜/'\n",
    "train_pickle_dir = './pickle/train/'\n",
    "test_pickle_dir = './pickle/A/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def 加工法人代表资产():\n",
    "    res = []\n",
    "    for data_dir,pickle_dir in [(train_dir,train_pickle_dir),(test_dir,test_pickle_dir)]:\n",
    "        if data_dir==train_dir:\n",
    "            法人代表AUM_T0 = pd.read_csv(os.path.join(train_dir,'XW_RPSTV_HOLD_PD.csv'))  \n",
    "        else:\n",
    "            法人代表AUM_T0 = pd.read_csv(os.path.join(test_dir,'XW_RPSTV_HOLD_PD_A.csv'))\n",
    "\t\t\t\n",
    "        法人代表AUM_T0.columns = ['数据日期','法定代表人客户ID','存款余额','活期存款余额','贷款余额','理财余额','基金余额','国债余额','保险余额','金融资产余额','AUM余额']\n",
    "\n",
    "        法人代表AUM_T0.sort_values(['法定代表人客户ID','数据日期'],inplace=True,ascending=True)\n",
    "\n",
    "        for a in list(法人代表AUM_T0.filter(regex='余额')):\n",
    "           法人代表AUM_T0[a] = pow((法人代表AUM_T0[a])/3.12,3).round(2)\n",
    "\n",
    "        法人代表AUM_T0['数据日期'] = 法人代表AUM_T0['数据日期'].astype('str')\n",
    "        法人代表AUM_T0['数据日期'] = 法人代表AUM_T0['数据日期'].astype('datetime64[ns]')\n",
    "        法人代表AUM_T0['数据日期']=pd.to_datetime(法人代表AUM_T0['数据日期'])+pd.DateOffset(days=11886)\n",
    "        \n",
    "        法人代表AUM_T0.drop_duplicates(inplace=True)\n",
    "\t\t\n",
    "        法人代表AUM_T0['资产汇总']=法人代表AUM_T0['存款余额']+法人代表AUM_T0['理财余额']+\\\n",
    "        法人代表AUM_T0['基金余额']+法人代表AUM_T0['国债余额']+法人代表AUM_T0['保险余额']\n",
    "\t\t\n",
    "        法人代表AUM_T0['资金潜力']=法人代表AUM_T0['资产汇总']-法人代表AUM_T0['AUM余额']\n",
    "\t\t\n",
    "        #法人代表AUM_T0.drop(['存款余额','理财余额','基金余额','国债余额','保险余额','金融资产余额','资产汇总'],axis=1,inplace=True)\n",
    "\n",
    "        法人代表AUM_T0['环比活期']=(法人代表AUM_T0.groupby(['法定代表人客户ID'])['活期存款余额'].diff(1)*100/\\\n",
    "\t\t(法人代表AUM_T0.groupby(['法定代表人客户ID'])['活期存款余额'].shift(1)+0.1)).round(0)\n",
    "\n",
    "        法人代表AUM_T0['环比AUM']=(法人代表AUM_T0.groupby(['法定代表人客户ID'])['AUM余额'].diff(1)*100/\\\n",
    "\t\t(法人代表AUM_T0.groupby(['法定代表人客户ID'])['AUM余额'].shift(1)+0.1)).round(0)\n",
    "\n",
    "        法人代表AUM_T1=法人代表AUM_T0.groupby(['法定代表人客户ID']).agg({'活期存款余额':['last','mean','std'],'贷款余额':['last','first']\\\n",
    "\t\t,'AUM余额':['last','mean','std'],'资金潜力':['sum'],'环比活期':['last','mean']\\\n",
    "        ,'环比AUM':['last','mean'],'数据日期':['count']})\n",
    "        法人代表AUM_T1.reset_index(inplace=True)\n",
    "        法人代表AUM_T1.columns = ['法定代表人客户ID','个人最新活期','个人平均活期','个人活期_std',\\\n",
    "\t\t'个人最新贷款','个人最早贷款','个人最新AUM','个人平均AUM','个人AUM_std',\\\n",
    "\t\t'个人资金潜力','个人环比活期_last','个人环比活期_mean','个人环比AUM_last','个人环比AUM_mean','个人账户存续期']\n",
    "\n",
    "        法人代表AUM_T1['已还贷款']=法人代表AUM_T1['个人最新贷款']-法人代表AUM_T1['个人最早贷款']\n",
    "        法人代表AUM_T1.drop(['个人最新贷款','个人最早贷款'],axis=1,inplace=True)\n",
    "\n",
    "        法人代表AUM_T1['个人活期_标准偏差'] = (100*法人代表AUM_T1['个人活期_std']/法人代表AUM_T1['个人平均活期']).round(0)\n",
    "        法人代表AUM_T1['个人AUM_标准偏差'] = (100*法人代表AUM_T1['个人AUM_std']/法人代表AUM_T1['个人平均AUM']).round(0)\n",
    "\t\t\n",
    "        if data_dir==train_dir:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(train_dir,'XW_TARGET.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID','违约标记']\n",
    "            目标客户列表.drop(['违约标记'],axis=1,inplace=True)\n",
    "        else:\n",
    "            目标客户列表 = pd.read_csv(os.path.join(test_dir,'XW_TARGET_A.csv'))\n",
    "            目标客户列表.columns = ['借款合同编号','客户ID','纳税人识别号','法定代表人客户ID']\n",
    "\n",
    "        法人代表AUM特征=目标客户列表.merge(法人代表AUM_T1,on=['法定代表人客户ID'],how='left')\n",
    "        法人代表AUM特征.drop(['借款合同编号','纳税人识别号','法定代表人客户ID'],axis=1,inplace=True)\n",
    "\n",
    "        pickle.dump(法人代表AUM特征, open(pickle_dir+'法人代表AUM特征.p', 'wb'))\n",
    "\n",
    "        res.append(法人代表AUM特征)\n",
    "    return res[0],res[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "法人代表资产_训练集,法人代表资产_测试集=加工法人代表资产()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50000, 16)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "法人代表资产_训练集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5061, 16)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "法人代表资产_测试集.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
